package com.gsit.misn.bilders;

import static com.gsit.misn.utils.ScreenShotUtil.captureScreenShot;

import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.gsit.misn.common.MisnExcuter;
import com.gsit.misn.domain.Misn;
import com.gsit.misn.domain.Patient;
import com.gsit.misn.domain.User;
import com.gsit.misn.utils.ExcelReader;
import com.gsit.misn.utils.PatientUtil;
import com.gsit.misn.utils.Reader;
import com.thoughtworks.selenium.DefaultSelenium;

public class UpdatePatientBilder implements PatientBilder<Misn> {

	private static Logger logger = LoggerFactory
			.getLogger(UpdatePatientBilder.class);

	private Misn misn;
	private DefaultSelenium selenium;
	private Reader<User, List<Patient>> reader;
	private Integer currNoOfRecords = 0;

	@Override
	public void excute(DefaultSelenium selenium, Misn misn) {
		this.misn = misn;
		this.selenium = selenium;
		if (MisnExcuter.UPDATE_ONLY == misn.getMisnExcuter()) {
			this.updatePatientFromExcel();
		}
		
		System.out.println("The validation is "+misn.getValidation());

	}

	private void updatePatientFromExcel() {

		reader = new ExcelReader();
		List<Patient> patients = reader.read(misn.getUser());
		this.excuteUpdatePage(patients);

	}

	public void excuteUpdatePage(List<Patient> patients) {

		for (Patient patient : patients) {
			currNoOfRecords++;

			logger.info("currNoOfRecords " + currNoOfRecords + " >>> "+ patient.toString());

			System.out.println(patient.toString());
			
			if(!misn.getValidation()){
				this.updatePageNavigation(selenium, patient);
			}

			

		}
		logger.info("Totla no of record are processed ..."
				+ currNoOfRecords);

	}

	private void updatePageNavigation(DefaultSelenium selenium, Patient patient) {
		//String editLink = "edit.php?id=117798";
		String editLink = misn.getMisnLinks().getEditUrl()+"?id="+patient.getId();
		System.out.println("The edit link is :  "+editLink);
		selenium.open(editLink);
		selenium.waitForPageToLoad("10000");
		PatientUtil.setPatientIntoSelenium(selenium, patient,misn.getMisnExcuter().getType());
		String button = misn.getMisnLinks().getUpdateBtnName();
		String element = "//input[@value='" + button + "']";
		boolean isElm = selenium.isElementPresent(element);
		if (isElm) {
			logger.info("This can be click.........");
			Date start = new Date();

			System.out.println("Apply wait Start");
			/*try {
				Thread.sleep((misn.getUser().getWaitForProcess()*1000)+((Math.round((Math.random()*30)))*1000));
				// captureScreenShot screen 
				if(misn.getScreenShotCapture()){
					captureScreenShot(patient.getPatientID());
				}
			} catch (InterruptedException e) {
				e.printStackTrace();
			}*/
			Date end = new Date();
			logger.info("Apply wait End  : "	+ (end.getTime() - start.getTime()) / 1000);
			selenium.click(element);
		} else {
			logger.error("No Add button found");
		}

		// selenium.waitForPageToLoad("20000");

	}

}
